package com.example.flower_shop.controller.admin;

import com.example.flower_shop.common.ApiResponse;
import com.example.flower_shop.dto.admin.AdminOverviewDTO;
import com.example.flower_shop.service.AdminService;
import com.example.flower_shop.service.OrderService;
import com.example.flower_shop.service.ProductService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/admin")
@PreAuthorize("hasRole('MERCHANT')")
@RequiredArgsConstructor
public class AdminController {

    private final OrderService orderService;
    private final ProductService productService;
    private final AdminService adminService;

    @GetMapping("/overview")
    public ApiResponse<Map<String, Object>> getOverview() {
        try {
            log.info("开始获取管理员概览数据");

            // 使用AdminService获取完整的概览数据
            AdminOverviewDTO overviewDTO = adminService.getOverview();

            log.info("获取数据成功 - 订单数: {}, 商品数: {}, 收入: {}",
                    overviewDTO.getOrderCount(),
                    overviewDTO.getProductCount(),
                    overviewDTO.getIncome());

            Map<String, Object> overview = new HashMap<>();
            overview.put("orderCount", overviewDTO.getOrderCount());
            overview.put("productCount", overviewDTO.getProductCount());
            overview.put("income", overviewDTO.getIncome());

            return ApiResponse.success(overview);
        } catch (Exception e) {
            log.error("获取管理员概览数据失败: ", e);
            throw e;
        }
    }
}